﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DTOService;
using System.Data;

namespace DAOService
{
    public class DAOKhachHang : IDAOKhachHang
    {
        Provider p = new Provider();

        public Object ConvertOneRowIntoADTO(DataTable dt, int i)
        {
            KhachHang temp = new KhachHang();
            temp.MaKH = dt.Rows[i]["MaKH"].ToString();
            temp.HoTen = dt.Rows[i]["HoTen"].ToString();
            temp.DiaChi = dt.Rows[i]["DiaChi"].ToString();
            temp.NgaySinh = dt.Rows[i]["NgaySinh"].ToString();
            temp.Email = dt.Rows[i]["Email"].ToString();
            temp.DienThoai = dt.Rows[i]["DienThoai"].ToString();
            temp.CMND = dt.Rows[i]["CMND"].ToString();
            temp.TienDatCoc = float.Parse(dt.Rows[i]["SoTienDatCoc"].ToString());
            temp.DanhDauXoa = int.Parse(dt.Rows[i]["DanhDauXoa"].ToString());
            return temp;
        }

        public KhachHang[] SelectKhachHang()
        {
            KhachHang[] arr;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_DanhSachKhachHang");
                arr = new KhachHang[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    arr[i]= (KhachHang)ConvertOneRowIntoADTO(dt, i);
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return arr;
        }

        public KhachHang[] SelectKhachHang(int startIndex, int numRows)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_DanhSachKhachHang_Limit", new string[] { "startIndex", "numRows" }, new object[] { startIndex, numRows });
            KhachHang[] rs = new KhachHang[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                rs[i] = (KhachHang)ConvertOneRowIntoADTO(dt, i);
            }
            return rs;
        }

        public string UpdateKhachHang(KhachHang parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_CapNhatThongTinKhachHang", new string[] { "makh", "hoten", "diachi", "ngaysinh", "email", "dienthoai", "cmnd", "tiendatcoc" }, new object[] { parr.MaKH, parr.HoTen, parr.DiaChi, parr.NgaySinh, parr.Email, parr.DienThoai, parr.CMND, parr.TienDatCoc });
            return dt.Rows[0][0].ToString();
        }

        public string DeleteKhachHang(KhachHang parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_XoaKhachHang", new string[] { "makh" }, new object[] { parr.MaKH });
            return dt.Rows[0][0].ToString();
        }

        public KhachHang GetKhachHang(string ma)
        {
            KhachHang temp = new KhachHang();
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_GetKhachHang", new string[] { "@ma" }, new object[] { ma });
                if (dt.Rows.Count != 0)
                    temp = (KhachHang)ConvertOneRowIntoADTO(dt, 0);
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return temp;
        }

        public KhachHang[] SearchKhachHang(string ten)
        {
            KhachHang[] arr;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_SearchKhachHang", new string[] { "@ten" }, new object[] { ten });
                arr = new KhachHang[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    arr[i] = (KhachHang)ConvertOneRowIntoADTO(dt, i);
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
            return arr;
        }

        public string InsertKhachHang(KhachHang parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_ThemKhachHang", new string[] { "hoten", "diachi", "ngaysinh", "email", "dienthoai", "cmnd", "tiendatcoc" }, new object[] { parr.HoTen, parr.DiaChi,parr.NgaySinh, parr.Email, parr.DienThoai, parr.CMND, parr.TienDatCoc });
            return dt.Rows[0][0].ToString();
        }

        public KhachHang[] SelectKhachHangTheoTen_Limit(string name,int startIndex,int numRows)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_DanhSachKhachHangTheoTen_Limit", new string[] { "name", "startIndex", "numRows" }, new object[] { name, startIndex, numRows });
            KhachHang[] rs = new KhachHang[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                rs[i] = (KhachHang)ConvertOneRowIntoADTO(dt, i);
            }
            return rs;
        }

        public int NumberOfKhachHang()
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_SoKhachHang");
            return int.Parse(dt.Rows[0][0].ToString());
        }

        public int NumberOfKhachHang(string name)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_SoKhachHangTheoTen", new string[] { "name" }, new object[] { name });
            return int.Parse(dt.Rows[0][0].ToString());
        }

        public KhachHang[] SelectKhachHangTheoTen(string name)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_DanhSachKhachHangTheoTen", new string[] { "name" }, new object[] { name });
            KhachHang[] rs = new KhachHang[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                rs[i] = (KhachHang)ConvertOneRowIntoADTO(dt, i);
            }
            return rs;
        }

        public int SoBangDiaKhachHangDangThue(KhachHang parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_SoBangDiaDangThue", new string[] { "makh" }, new object[] { parr.MaKH });
            return int.Parse(dt.Rows[0][0].ToString());
        }
    }
}
